package com.intel.two.service.record.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.mapper.recordMapper.PurchaseHouseRecordMapper;
import com.intel.two.pojo.record.PurchaseHouseRecord;
import com.intel.two.service.record.PurchaseHouseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class PurchaseHouseRecordServiceImpl implements PurchaseHouseRecordService {
    @Autowired
    private PurchaseHouseRecordMapper purchaseHouseRecordMapper;
    @Override
    public LayuiPageVo<PurchaseHouseRecord> purchasehouseList(Map<String, Object> params) {
        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<PurchaseHouseRecord> informationRecords =  purchaseHouseRecordMapper.selectAllRecord(params);

        // mybatis 的分页数据结构
        PageInfo<PurchaseHouseRecord> recordPageInfo = new PageInfo<>(informationRecords);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        LayuiPageVo<PurchaseHouseRecord> recordLayuiPageVo = new LayuiPageVo<>();
        recordLayuiPageVo.setCode(0);
        recordLayuiPageVo.setMsg("购房信息分页列表数据");
        recordLayuiPageVo.setCount(recordPageInfo.getTotal());  // 总记录数
        recordLayuiPageVo.setData(recordPageInfo.getList());  // 分页列表数据

        return recordLayuiPageVo;

    }

    @Override
    public int purchasehouseAdd(PurchaseHouseRecord purchaseHouseRecord) {
        return purchaseHouseRecordMapper.purchasehouseAdd(purchaseHouseRecord);
    }

    @Override
    public int deletePurchasehouse(Integer id) {
        return purchaseHouseRecordMapper.deletePurchasehouse(id);
    }

    @Override
    public int deletePurchasehouseList(String ids) {
        String[] split = ids.split(",");
        Integer[] id = new Integer[split.length];
        int count=0;
        for (String s : split) {
            Integer integer = Integer.valueOf(s);
            id[count]=integer;
            count++;

        }
        return purchaseHouseRecordMapper.deletePurchasehouseList(id);
    }

    @Override
    public int update(PurchaseHouseRecord purchaseHouseRecord) {

        return purchaseHouseRecordMapper.update(purchaseHouseRecord);
    }
}
